var flowlistTabid;

/**
 * 工作流程列表
 * 
 * @return {}
 */
function workflowlist(tabid) {
	flowlistTabid = tabid;

	var store = new Ext.data.JsonStore({
				root : 'flowList',
				totalProperty : 'totalCount',
				idProperty : 'CG_ID',
				remoteSort : true,

				fields : ['PAGINATION_NUMBER', 'CG_ID', 'CG_KEY', 'CG_NAME',
						'CG_MEMO', 'OBJ_NAME', 'SURPORT'],
				proxy : new Ext.data.ScriptTagProxy({
							url : context
									+ '/system/flowmanage.do?method=flowlist'
						})
			});

	function renderTopic(value, p, record) {
		return String
				.format(
						'<b><a href="javascript:void(0);" onclick="javascript:editflow(\'{1}\', \'{0}\');">{0}</a></b>',
						value, record.data.CG_ID);
	}

	function renderLast(value, p, r) {
		var str = '<a href="javascript:void(0);" onclick="javascript:handleDraw(\'{0}\',\'{1}\');">流程图</a>';
		str += '&nbsp;<a href="javascript:void(0);" onclick="javascript:handleSet(\'{0}\',\'{1}\');">环节设置</a>';
		str += '&nbsp;<a href="javascript:void(0);" onclick="javascript:handleInit(\'{0}\',\'{1}\');">初始化</a>';
		return String.format(str, r.data.CG_ID, r.data['CG_NAME']);
	}

	var grid = new Ext.grid.GridPanel({
				id : tabid,
				title : '工作流程列表',
				store : store,
				loadMask : true,
				closable : true,
				layout : 'fit',
				// grid columns
				columns : [new Ext.grid.RowNumberer(), {
							id : 'fname',
							header : "流程类别名称",
							dataIndex : 'CG_NAME',
							align : 'left',
							width : 150,
							renderer : renderTopic,
							sortable : true
						}, {
							header : "唯一标识",
							dataIndex : 'CG_KEY',
							align : 'left',
							width : 100,
							sortable : false
						}, {
							id : 'oname',
							header : "流转对象",
							dataIndex : 'OBJ_NAME',
							align : 'left',
							width : 100,
							sortable : true
						}, {
							header : "运行系统",
							dataIndex : 'SURPORT',
							align : 'left',
							width : 100,
							sortable : true
						}, {
							header : "流程类别描述",
							dataIndex : 'CG_MEMO',
							width : 150,
							align : 'left',
							sortable : false
						}, {
							header : "操作设置",
							dataIndex : 'CG_ID',
							align : 'center',
							width : 150,
							renderer : renderLast,
							sortable : false
						}],

				stripeRows : true,
				autoExpandColumn : 'CG_NAME',
				viewConfig : {
					forceFit : true
				},
				border : false,
				// config options for stateful behavior
				stateful : true,
				stateId : 'grid',
				loadMask : {
					msg : "数据加载中，请稍等..."
				},

				// paging bar on the bottom
				bbar : new Ext.PagingToolbar({
							pageSize : 25,
							store : store,
							displayInfo : true,
							displayInfo : true,
							displayMsg : '总记录数 {0} - {1} of {2}',
							emptyMsg : "没有数据"
						}),
				tbar : [{
							text : '新建工作流程',
							iconCls : 'addflow',
							handler : function() {
								neuflow();
							}
						}, '-', {
							id : 'delflowbutton',
							text : '删除工作流程',
							iconCls : 'delflow',
							disabled : true,
							handler : function() {
								deleteflow(grid);
							}
						}, '-', {
							id : 'copyflowbutton',
							text : '复制工作流程',
							iconCls : 'copyflow',
							disabled : true,
							handler : function() {
								if (grid.getSelectionModel().hasSelection()) {
									var selectedNodes = grid
											.getSelectionModel()
											.getSelections();

									if (selectedNodes
											&& selectedNodes.length > 0) {
										copyflow(selectedNodes[0]);
									}
								}
							}
						}]
			});

	grid.on('click', function(e) {
				if (grid.getSelectionModel().hasSelection()) {
					Ext.getCmp('delflowbutton').enable();
					Ext.getCmp('copyflowbutton').enable();
				} else {
					Ext.getCmp('delflowbutton').disable();
					Ext.getCmp('copyflowbutton').disable();
				}
			});

	grid.setIconClass('tabs');

	// trigger the data store load
	store.load({
				params : {
					start : 0,
					limit : 25
				}
			});

	return grid;
}

/**
 * 刷新流程列表
 */
function reloadFlowListStore() {
	if (Ext.get(flowlistTabid)) {
		Ext.getCmp(flowlistTabid).getStore().reload();
	}
}

/**
 * 删除操作确认
 * 
 * @param {}
 *            grid
 */
function deleteflow(grid) {
	if (grid.getSelectionModel().hasSelection()) {
		Ext.MessageBox.confirm('提示', '确定删除该流程？', handleDeleteFlow);
	}
}

/**
 * 删除流程定义
 * 
 * @param {}
 *            btn
 */
function handleDeleteFlow(btn) {
	if (btn == 'yes') {
		var id;
		var selectedNodes = Ext.getCmp(flowlistTabid).getSelectionModel()
				.getSelections();

		if (selectedNodes && selectedNodes.length > 0) {
			id = selectedNodes[0].data.CG_ID;
		}

		Ext.Ajax.request({
					url : context + "/system/flowmanage.do?method=deleteflow",
					method : 'POST',
					async : false,
					params : {
						gid : id
					},
					success : function(response, options) {
						var o = Ext.util.JSON.decode(response.responseText);
						if (!o.success) {
							Ext.Msg.alert('提示', '删除流程定义失败！');
						} else {
							Ext.Msg.alert('提示', '已成功删除流程定义！');
							reloadFlowListStore();
						}
					}
				})
	}
}

function handleInit(id, name) {
	Ext.MessageBox.confirm('提示', '初始化流程[' + name
					+ ']将清空流程活动相关的各项数据,流程初始化操作执行后，被清理的数据将不可恢复？', function(btn) {
				if (btn == 'yes') {
					var url = context + "/system/flowmanage.do?method=reinit";

					Ext.Ajax.request({
						url : url,
						method : 'POST',
						async : false,
						params : {
							id : id
						},
						success : function(response, options) {
							var o = Ext.util.JSON.decode(response.responseText);
							if (!o.success) {
								Ext.Msg.alert('提示', '初始化流程失败！');
							} else {
								Ext.Msg.alert('提示', '初始化流程成功！');
							}
						}
					})
				}
			});
}

/**
 * 构建流程图
 * 
 * @param {}
 *            id
 */
function handleDraw(id) {
	var url = context
			+ "/system/flowmanage.do?method=preplotter&type=drawing&id=" + id;
	window
			.showModalDialog(
					url,
					"",
					"dialogWidth:800px;dialogHeight:600px;center:yes;help:no;scroll:no;status:no;resizable:yes;");
}

/**
 * 环节设置
 * 
 * @param {}
 *            id
 * @param {}
 *            name
 */
function handleSet(id, name) {
	stepList(id, name);
}